#ifndef __QpwServer__
#define __QpwServer__

#include <QtGui/QTreeWidget>
#include <QpwAccountManager.h>
#include <QpwNetwork.h>
#include <QpwSubType.h>
#include <QyGLInputController.h>
#include <QyGLViewWidget.h>
#include "QpwWorld.h"

class QpwAdminTools : protected QTreeWidget, public QyGL::InputController {
    Q_OBJECT
public:
    explicit QpwAdminTools( QpwWorld::Processor * processor, QyGL::Scene * scene = 0 );

    void setSubTypeDatabase( QpwSubTypeDatabase * subTypeDatabase );
    QpwSubType selectedSubType() const;

    void show() { QTreeWidget::show(); }

private slots:
    void on_itemActivated( QTreeWidgetItem * item, int column );

private:
    void mousePressd( const QyGL::MouseEvent & event );
    void mouseMoved( const QyGL::MouseEvent & event );
    void mouseReleased( const QyGL::MouseEvent & event );

    QpwSubTypeDatabase * p_subTypeDatabase;
    QpwWorld::Processor * p_processor;

};


namespace QpwWorld {

class DecorationObject;
class DynamicObject;

class Player {
public:
    enum Status {
        Connected,
        WaitObjects,
        Playing,
        Pause,
    };
    explicit Player() : account(0), object(0), status(Connected) {}

    QpwAccount * account;
    QpwWorld::DynamicObject * object;
    Status status;
    MovingController movingController;

};

};

class QpwServer : private QpwWorld::Processor {
    Q_OBJECT
public:
    explicit QpwServer( QObject * parent = 0 );
    virtual ~QpwServer();

private slots:
    void on_server_clientAuthorised( QpwNetwork::ClientRecord * clientRecord );
    void on_server_clientDisconnected( QpwNetwork::ClientRecord * clientRecord );
    void datagramReceived( QyNet::AbstractDatagramEncoder * datagram );

private:
    bool readConfig();
    bool startWorld();
    void send( QyNet::AbstractDatagramEncoder * datagram );

    QpwNetwork::Server server;
    QpwAccountManager accountManager;
    QString rootDir,
            dataDirPath;
    quint16 port;
    QHostAddress hostmask;
    QyGL::ViewWidget viewWidget;
    QMap<QUuid,QpwWorld::Player> players;
    QpwSubType playerSubType;
    QpwAdminTools * adminTools;

};

#endif//__QpwServer__
